using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ExpertPdf;
using Scb.Framework;
using Robot.Business;
using Robot.Data;
using Robot.Entities;
using System.EnterpriseServices;
using System.IO;
using System.DirectoryServices;

namespace WebRobot.App_code
{
  public class BasePage : Page
  {
    
    public static string PdfToText(string sFileName)
    {
      string s = "";
      try
      {
        string _licenseKey = "i6C6q7m9q7m/vau5pburuLqlurmlsrKysg==";
        TextLayout textLayout = TextLayout.OriginalLayout;
        System.Text.Encoding textEncoding = System.Text.Encoding.UTF8;
        textEncoding = System.Text.Encoding.GetEncoding("utf-8");
        PdfToTextConverter pdfToTextConverter = new PdfToTextConverter();
        pdfToTextConverter.LicenseKey = _licenseKey;
        pdfToTextConverter.Layout = textLayout;
        pdfToTextConverter.StartPageNumber = 1;
        pdfToTextConverter.EndPageNumber = 0;
        s = pdfToTextConverter.ConvertToText(sFileName);
      }
      catch (Exception ex)
      {
        Logger.Error(ex);
        throw;
      }
      return s;
    }

    #region Function Login page

    public tblUser CurrentUser
    {
      get { return (tblUser)Session["CURRENT_USER"]; }
      set { Session["CURRENT_USER"] = value; }
    }
    
    public string DoLogin(string sUsername, string sPassword, bool isLDAP)
    {
      if (sUsername != null)
      {
        DirectoryEntry entry;
        CurrentUser = new tblUser();
        try
        {
          if (isLDAP)
          {
            //entry = new DirectoryEntry("LDAP://zone1.scb.net/DC=zone1,DC=scb,DC=net", sUsername, sPassword);
            //string name = entry.Name;
          }

          tblUser user = new tblUser();
          user = tblUserService.GettblUser(sUsername);
          if (user != null)
          {
            user.LastLoginDate = DateTime.Now;
            CurrentUser = user;
            tblUserService.UpdatetblUser(user);
            CreateAuthenticationTicket(sUsername);

            return "Success";
          }
          else
          {
            CurrentUser = null;
            return "User not exist";
          }
        }
        catch (Exception ex)
        {
          Logger.Error("WebRobot.App_code.BasePage::Login Error:", ex);
          return "Login failed! Please try again.";
        }
      }
      return null;
    }

    private string LoginLocal(string sUserName)
    {

      tblUser user = tblUserService.GettblUser(sUserName);
      if (user.Peoplewise == sUserName)
        return "Sucess";
      return "";
    }

    public void CreateAuthenticationTicket(string clientId)
    {
      FormsAuthentication.SetAuthCookie(clientId, false);
      FormsAuthenticationTicket clientTicket =
         new FormsAuthenticationTicket(1,
              "Name",
              DateTime.Now,
              DateTime.Now.AddDays(1),
              false,
              clientId);
      HttpCookie cookie = new HttpCookie(
        FormsAuthentication.FormsCookieName,
        FormsAuthentication.Encrypt(clientTicket));
      Response.Cookies.Add(cookie);
    }

    #endregion

    private tblOpicCode GetOpicsCode(string sBranch, string sCusName)
    {
      return tblOpicCodeService.GettblOpicCode(sCusName, sBranch);
    }

    public string GetValueInXML(string sName)
    {

      try
      {
        return ConfigurationManager.AppSettings[sName];
      }
      catch (Exception ex)
      {
        Logger.Error("WebRobot.App_code.BasePage::GetValueInXML", ex);
        throw;
      }

    }

  }

}
